package com.runa.monitor.platinterface.netservice.equipment;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.Archive;
import com.runa.monitor.comm.dto.Node;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.persistence.task.RequestPendingTask;
import com.runa.persistence.task.TaskTypeEnum;
import com.runa.rccprotocol.dto.equdata.EquData;
import com.runa.rccprotocol.dto.guangda.GdRequestData;
import com.runa.rccprotocol.dto.param.ExtendParam;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 2024/7/12 14:14
 * zhangYB
 */
@Slf4j
public class GdDriver extends NetEqu<String> {

    private final AtomicInteger clientId = new AtomicInteger(0);

    public GdDriver(String serial, Integer timeout) {
        super(serial, timeout);
    }


    @Override
    public void readNodeEqu(List<Node> nodes, TaskFunction function, EquData.EquDataType type) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, nodes);
            return;
        }
        switch(type) {
            case METER:
                readMeterByNo(nodes, function, TaskTypeEnum.READ_METER);
                break;
            case PUBLIC_UNIT:
            case VALVE:
                readMeterByNo(nodes, function, TaskTypeEnum.READ_VALVE);
                break;
            case TEMPERATURE_PANEL:
//                readPanel(nodes, function, type);
                break;
        }

    }

    private void readMeterByNo(List<Node> nodes, TaskFunction function, TaskTypeEnum type) {
        for (Node node : nodes) {
            int i = clientId.incrementAndGet();
            GdRequestData gdRequestData = new GdRequestData(serial(), node.getSerial(), i);
            gdRequestData.requestDataByNo();
            gdRequestData.addNode(node);
            gdRequestData.requestDataByNo();
            log.debug("{} 光大集中器请求报文: {}", serial(), gdRequestData.getMessage());
            gdRequestData.setRemain(Collections.singletonList(node));
            sendRequest(gdRequestData, function, new RequestPendingTask(type, node));
        }
    }


    @Override
    public void valveOnOffControl(List<Node> nodeList, boolean ifLock, Integer opening, TaskFunction function) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, nodeList);
            return;
        }
        for (Node node : nodeList) {
            GdRequestData gdRequestData = new GdRequestData(serial(), node.getSerial());
            gdRequestData.addNode(node);
            gdRequestData.setRemain(node);
            gdRequestData.valveOpen(opening != null && (opening != 0), (byte)0, (byte)(node.getPort() & 0xff));
            log.debug("{} 光大请求阀控:  {} {}", serial(), gdRequestData.getMessage(), JSON.toJSONString(function));
            sendRequest(gdRequestData, function);
        }

    }

    @Override
    public void setNodeEquArchive(List<Archive> archives, TaskFunction function) {

    }

    @Override
    public void clearNodeEquArchive(TaskFunction function) {

    }


    @Override
    public void readNodeRecord(List<Integer> indexes, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitRecord(List<Node> nodeList, Integer model, Map param, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitModel(List<Node> nodeList, boolean model, TaskFunction function) {

    }

    @Override
    public void sentExtendParam(ExtendParam param, TaskFunction function) {

    }

    @Override
    public void readPublicUnitRecord(Node valve, Integer model, TaskFunction function) {

    }

    @Override
    public void readPublicUnitAndMeter(Node valve, Node meter, TaskFunction function) {

    }

    @Override
    public void heartbeatUpdate() {

    }

    @Override
    public boolean heartStop() {
        return false;
    }
}
